package com.guli.servicevod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.guli.commonutils.R;
import com.guli.servicebase.exceptionhandler.LingqiException;
import com.guli.servicevod.service.VideoService;
import com.guli.servicevod.utils.ConstantVodUtils;
import com.guli.servicevod.utils.InitClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
@RequestMapping("/vod/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @PostMapping("/uploadAlyVideo")
    public R uploadAlyVideo(MultipartFile file) {
        String videoId = videoService.uploadAlyVideo(file);
        return R.ok().data("videoId", videoId);
    }

    @DeleteMapping("/removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable String id) {
        try {
            DefaultAcsClient client = InitClient.initClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);

            DeleteVideoRequest request = new DeleteVideoRequest();

            request.setVideoIds(id);

            client.getAcsResponse(request);
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new LingqiException(20001, "删除视频失败");
        }
    }
}
